﻿
using Infrastructure.Extend;
using MyData.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyData.DB
{
   public class MyDBContext:DbContext
    {
        public IDbSet<Depart> Departs { get; set; }
        public IDbSet<UserInfo> UserInfos { get; set; }
        public IDbSet<AdminUser> AdminUsers { get; set; }
        public IDbSet<Menu> Menus { get; set; }
        public IDbSet<AccessRecord> AccessRecords { get; set; }
        public IDbSet<Mobile> Mobiles { get; set; }
        public IDbSet<DepartMobileRL> DepartMobileRLs { get; set; }
        public IDbSet<Visitor> Visitors { get; set; }
        public IDbSet<SystemConfig> SystemConfigs { get; set; }
        public IDbSet<SyncRL> SyncRLs { get; set; }
        public DbSet<MeetUser> MeetUsers { get; set; }
        public DbSet<Meet> Meets { get; set; }
        public DbSet<Holiday> Holidays { get; set; }
        public DbSet<CarRecord> CarRecords { get; set; }
        public DbSet<IPCamera> IPCameras { get; set; }
        public DbSet<SyncError> SyncErrors { get; set; }
        public MyDBContext():base("name=MyDbContext")//这里根据配置文件连接数据库
        {
            Database.SetInitializer(new Init());//创建数据库
            this.Configuration.AutoDetectChangesEnabled = false;//关闭自动跟踪对象的属性变化
            this.Configuration.LazyLoadingEnabled = false; //关闭延迟加载
            this.Configuration.ProxyCreationEnabled = false; //关闭代理类
            this.Configuration.ValidateOnSaveEnabled = false; //关闭保存时的实体验证
            this.Configuration.UseDatabaseNullSemantics = true; //关闭数据库null比较行为
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
            var mappings = GetType().Assembly.GetInheritedTypes(typeof(EntityTypeConfiguration<>));//这里获取所有entity
            foreach (var mapping in mappings)
            {
                dynamic instance = Activator.CreateInstance(mapping);
                modelBuilder.Configurations.Add(instance);
            }
        }

        public static string GetConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["MyDbContext"].ConnectionString;
        }


        public override int SaveChanges()
        {
            try
            {
                return base.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                var sb = new StringBuilder();
                foreach (var error in ex.EntityValidationErrors)
                {
                    foreach (var item in error.ValidationErrors)
                    {
                        sb.AppendLine(item.PropertyName + ": " + item.ErrorMessage);
                    }
                }
              
                throw;
            }
        }
    }
}
